//
//  ZSearchBar.m
//  TestSearchBar
//
//  Created by 朱志佳 on 2018/4/10.
//  Copyright © 2018年 zhuzj. All rights reserved.
//

#import "ZSearchBar.h"
#import "ZZJHeader.h"


/*
 使用Demo参考
 //LLSearchBar *searchBar = [[LLSearchBar alloc] initWithFrame:CGRectMake(15, self.largeTitle.bottom, IPHONEWIDTH - 15 - 50, 36)];
 
 LLSearchBar *searchBar = [[LLSearchBar alloc] initWithFrame:CGRectMake(15, self.largeTitle.bottom, IPHONEWIDTH - 15 - 50, 36) searchBarBackgroundImageColor:[UIColor whiteColor] cursorColor:FBEE_COLOR_ASSISTANT searchIcon:[UIImage imageNamed:@"search"] textFieldBackgroundColor:FBEE_COLOR_TEXTFIELD_BACKGROUND textFieldCornerRadius:5.0f textFieldBorderColor:FBEE_COLOR_TEXTFIELD_BACKGROUND textFieldBorderWidth:1.0f textFieldTextColor:COLOR_TEXT_BLACK textFieldTextFont:[UIFont systemFontOfSize:14.0] textFieldAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"搜索or创建Bee圈" attributes:@{NSForegroundColorAttributeName:FBEE_COLOR_UNTOUCH}]];
 */

@interface ZSearchBar()

@property (nonatomic,strong)UIColor *searchBarBackgroundImageColor;

@end


@implementation ZSearchBar

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self modifyStyleByTraversal];
    }
    return self;
}

- (instancetype)initWithFrameForFBee:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self modifyStyleByTraversal];
    }
    return self;
}

//自定义样式 全参数
- (instancetype)initWithFrame:(CGRect)frame
searchBarBackgroundImageColor:(UIColor *)searchBarBackgroundImageColor
                  cursorColor:(UIColor *)cursorColor
                   searchIcon:(UIImage *)searchIcon
     textFieldBackgroundColor:(UIColor *)textFieldBackgroundColor
        textFieldCornerRadius:(CGFloat)textFieldCornerRadius
         textFieldBorderColor:(UIColor *)textFieldBorderColor
         textFieldBorderWidth:(CGFloat)textFieldBorderWidth
           textFieldTextColor:(UIColor *)textFieldTextColor
            textFieldTextFont:(UIFont *)textFieldTextFont
textFieldAttributedPlaceholder:(NSAttributedString *)textFieldAttributedPlaceholder {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.searchBarBackgroundImageColor = searchBarBackgroundImageColor;
        [self modifyStyleBySettingSearchBarBackgroundImageColor:searchBarBackgroundImageColor cursorColor:cursorColor searchIcon:searchIcon textFieldBackgroundColor:textFieldBackgroundColor textFieldCornerRadius:textFieldCornerRadius textFieldBorderColor:textFieldBorderColor textFieldBorderWidth:textFieldBorderWidth textFieldTextColor:textFieldTextColor textFieldTextFont:textFieldTextFont textFieldAttributedPlaceholder:textFieldAttributedPlaceholder];
    }
    return self;
}


#pragma mark -此处是通过拿到TextField后 进行设置的，也即可以通过拿到textField进行搜索框 自定义样式 -z
//样式设计
- (void)modifyStyleByTraversal{
    
    // 修改搜索框光标颜色
    self.tintColor = FBEE_COLOR_ASSISTANT;
    // 修改搜索框的搜索图标
    [self setImage:[UIImage imageNamed:@"search"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    
    UITextField *textField = nil;
    textField = [self getSearchTextField];
    
    if (textField == nil) {
        return;
    }
    
    //设置输入框的背景颜色
    textField.clipsToBounds = YES;
    textField.backgroundColor = FBEE_COLOR_TEXTFIELD_BACKGROUND;
    //设置输入框边框的圆角以及颜色
    textField.layer.cornerRadius = 5.0f;
    textField.layer.borderColor = FBEE_COLOR_TEXTFIELD_BACKGROUND.CGColor;
    textField.layer.borderWidth = 1;
    //设置输入字体颜色
    textField.textColor = COLOR_TEXT_BLACK;
    //设置默认文字颜色
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"搜索or创建Bee圈" attributes:@{NSForegroundColorAttributeName:FBEE_COLOR_UNTOUCH}];
}

//searchBarBackgroundImageColor
//cursorColor
//searchIcon
//textFieldBackgroundColor
//textFieldCornerRadius
//textFieldBorderColor
//textFieldBorderWidth
//textFieldTextColor
//textFieldTextFont
//textFieldAttributedPlaceholder
- (void)modifyStyleBySettingSearchBarBackgroundImageColor:(UIColor *)searchBarBackgroundImageColor
                                              cursorColor:(UIColor *)cursorColor
                                               searchIcon:(UIImage *)searchIcon
                                 textFieldBackgroundColor:(UIColor *)textFieldBackgroundColor
                                    textFieldCornerRadius:(CGFloat)textFieldCornerRadius
                                     textFieldBorderColor:(UIColor *)textFieldBorderColor
                                     textFieldBorderWidth:(CGFloat)textFieldBorderWidth
                                       textFieldTextColor:(UIColor *)textFieldTextColor
                                        textFieldTextFont:(UIFont *)textFieldTextFont
                           textFieldAttributedPlaceholder:(NSAttributedString *)textFieldAttributedPlaceholder
{
    // 修改搜索框光标颜色
    self.tintColor = cursorColor;
    // 修改搜索框的搜索图标
    [self setImage:searchIcon forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    
    UITextField *textField = nil;
    textField = [self getSearchTextField];
    
    if (textField == nil) {
        return;
    }
    
    //设置输入框的背景颜色
    textField.clipsToBounds = YES;
    textField.backgroundColor = textFieldBackgroundColor;
    //设置输入框边框的圆角以及颜色
    textField.layer.cornerRadius = textFieldCornerRadius;
    textField.layer.borderColor = textFieldBorderColor.CGColor;
    textField.layer.borderWidth = textFieldBorderWidth;
    //设置输入字体颜色
    textField.textColor = textFieldTextColor;
    //设置输入字体
    textField.font = textFieldTextFont;
    //设置默认文字颜色
    textField.attributedPlaceholder = textFieldAttributedPlaceholder;
    
}


//补充：修改UISearchBar宽高
//UISearchBar 有默认的布局样式，我们想要修改他的宽高，需要重写layoutSubviews 方法。然后在这个方法里面修改UISearchBarTextField的宽高。
//实现代码
- (void)layoutSubviews{
    [super layoutSubviews];
    
    //修改搜索框背景图片为自定义的灰色
    UIColor *backgroundImageColor = [UIColor whiteColor];
    if (self.searchBarBackgroundImageColor) {
        backgroundImageColor = self.searchBarBackgroundImageColor;
    }
    //FbeeLog(@"self.height:%lf",self.height);
    UIImage* clearImg = [self imageWithColor:backgroundImageColor andHeight:self.height];
    self.backgroundImage = clearImg;
    
    UITextField *textField = [self valueForKey:@"searchBarTextField"];
    if (textField) {
        textField.frame = CGRectMake(0, 0, self.width, self.height);
    }
}



// 画指定高度和颜色的图片
- (UIImage*) imageWithColor:(UIColor*)color andHeight:(CGFloat)height {
    CGRect rect = CGRectMake(0.0, 0.0, 1.0, height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


#pragma mark -获取searchBar的TextField
// 通过遍历self.subviews找到textField
- (UITextField *)getSearchTextField{
    
    //在init方法中查找的方法 可以查找到
    UITextField *textField = nil;
    for (UIView *view in self.subviews.lastObject.subviews) {
        if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
            textField = (UITextField *)view;
        }
        
        //        if ([view isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
        //            UIButton *cancel = (UIButton *)view;
        //            [cancel setTitle:@"取消" forState:UIControlStateNormal];
        //            [cancel setTitleColor:FBEE_COLOR_BLUE forState:UIControlStateNormal];
        //        }
    }
    
    
    //在layoutSubViews中查找的方法 可以查找到
    if (textField == nil) {
        NSUInteger numViews = [self.subviews count];
        for(int i = 0;i < numViews; i++) {
            if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
                textField = [self.subviews objectAtIndex:i];
            }
        }
        // 如果上述方法找不到searchField,试试下面的方法
        if (textField == nil) {
            NSArray *arraySub = [self subviews];
            UIView *viewSelf = [arraySub objectAtIndex:0];
            NSArray *arrayView = [viewSelf subviews];
            for(int i = 0;i < arrayView.count; i++) {
                if([[arrayView objectAtIndex:i] isKindOfClass:[UITextField class]]) {
                    textField = [arrayView objectAtIndex:i];
                }
            }
        }
    }
    
    if (textField == nil) {
        textField = [self valueForKey:@"searchBarTextField"];
    }
    
    return textField;
}


@end
